#ifndef DEFINES_H
#define DEFINES_H

#define USE_PACKAGE
//#define USE_DEBUG

#define BUFF_SIZE 5100
#define BLOCK_SIZE 5000

static int ReadINT32(byte* buff, int offset)
{
	int result = 0;
	memcpy(&result, buff + offset, 4);
	return result;
}


static void WriteINT32(int n, byte* buff, int offset)
{
	memset(buff + offset, 0, 4);
	memcpy(buff + offset, &n, 4);
}

static short ReadINT16(byte* buff, int offset)
{
	short result = 0;
	memcpy(&result, buff + offset, 2);
	return result;
}


static void WriteINT16(short n, byte* buff, int offset)
{
	memset(buff + offset, 0, 2);
	memcpy(buff + offset, &n, 2);
}

static byte ReadINT8(byte* buff, int offset)
{
	return buff[offset];
}


static void WriteINT8(byte n, byte* buff, int offset)
{
	buff[offset] = n;
}

struct Package
{
	byte type;
	byte action;
	int dataSize;
	int dataOffset;
	byte data[BUFF_SIZE - 10];
	Package()
	{
		OutputDebugString(_T("[Client] Package constructor!"));
		
		//dataOffset = sizeof(byte) + sizeof(byte) + sizeof(int);
		dataOffset = 6;
	}
	void LoadData(byte* buff, int offset = 0)
	{
		int _offset = offset;

		type = ReadINT8(buff, _offset);
		_offset += 1;

		action = ReadINT8(buff, _offset);
		_offset += 1;

		dataSize = ReadINT32(buff, _offset);
		_offset += 4;

		memcpy(data, buff + _offset, dataSize);
	}

	int WriteToBuffer(byte* buff)
	{
		int offset = 0;

		WriteINT8(type, buff, offset);
		offset++;

		WriteINT8(action, buff, offset);
		offset++;

		if(dataSize < 0 || dataSize > sizeof(data))
			dataSize = sizeof(data);
		WriteINT32(dataSize, buff, offset);		
		offset+=4;

		//memcpy(buff + offset, data, sizeof(data));
		memcpy(buff + offset, data, dataSize);

		return offset + dataSize;
	}
};

enum TYPE
{
	CHATTING = 1,
	FILE_TRANSFER = 2, 
};

enum ACTION
{
	CHAT_SIGNIN = 1,
	CHAT_SEND_MESSAGE = 2,
	CHAT_SIGNOUT = 3,

	FILE_HEADER = 1,
	FILE_START = 2,
	FILE_SEND = 3,
	FILE_STOP = 4,
};

class Defines
{
public:
	//static const int BUFF_SIZE = 1024;
	static const char SIGN_IN = '1';
	static const char SIGN_OUT = '2';
	static const char SEND = '3';
	static const char SENDFILE_REQUEST = '4';
	static const char SENDING_FILE = '5';
};
#endif